use sea_orm_migration::{prelude::*};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Todos::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Todos::Id)
                            .uuid()
                            .not_null()
                            .primary_key()
                            .default(SimpleExpr::FunctionCall(PgFunc::gen_random_uuid()))
                            .comment("Todo唯一标识符")
                    )
                    .col(
                        ColumnDef::new(Todos::Title)
                            .string_len(100)
                            .not_null()
                            .default("")
                            .comment("Todo标题")
                    )
                    .col(
                        ColumnDef::new(Todos::Description)
                            .text()
                            .comment("Todo描述")
                    )
                    .col(
                        ColumnDef::new(Todos::Status)
                            .char_len(15)
                            .not_null()
                            .default("pending")
                            .comment("Todo状态")
                    )
                    .col(
                        ColumnDef::new(Todos::CreatedAt)
                            .timestamp()
                            .not_null()
                            .comment("创建时间")
                    )
                    .col(
                        ColumnDef::new(Todos::UpdatedAt)
                            .timestamp()
                            .not_null()
                            .comment("更新时间")
                    )
                    .to_owned()
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Todos::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Todos {
    Table,
    Id,
    Title,
    Description,
    Status,
    CreatedAt,
    UpdatedAt,
}